﻿namespace Rzrlf.SP.Innovator.WebParts.StructureBrowserWebPart
{
    [System.ComponentModel.ToolboxItemAttribute(false)]
    public class StructureBrowserWebPart : System.Web.UI.WebControls.WebParts.WebPart, Rzrlf.SP.Innovator.WebParts.IStructureData
    {
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/Rzrlf.SP.Innovator.WebParts/StructureBrowserWebPart/StructureBrowserWebPartUserControl.ascx";

        private Rzrlf.SP.Innovator.WebParts.StructureBrowserWebPart.StructureBrowserWebPartUserControl _control = null;
        private System.Web.UI.UpdatePanel _updatePanel = null;

        public StructureBrowserWebPart()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            _control = this.Page.LoadControl(_ascxPath) as Rzrlf.SP.Innovator.WebParts.StructureBrowserWebPart.StructureBrowserWebPartUserControl;

            _updatePanel = new System.Web.UI.UpdatePanel();
            _updatePanel.ContentTemplateContainer.Controls.Add(_control);

            base.Controls.Add(_updatePanel);
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderContents(writer);
        }

        #region IStructureData Implementation...

        public string CurrentItemXml
        {
            get
            {
                System.Web.UI.WebControls.HiddenField field = Rzrlf.SP.Innovator.WebParts.Utils.ControlUtil.FindControlRecursivelyById(base.Page, "hdnCurrentItem") as System.Web.UI.WebControls.HiddenField;
                if (field != null)
                {
                    return field.Value;
                }
                return string.Empty;
            }
        }

        public System.Collections.Generic.Dictionary<System.Web.UI.Control, string> TriggerControls
        {
            get
            {
                return _control.TriggerControls;
            }
        }

        #endregion

        #region Web Part Communication Implementation...

        [System.Web.UI.WebControls.WebParts.ConnectionProvider("Structure Browser")]
        public Rzrlf.SP.Innovator.WebParts.IStructureData GetStructureData()
        {
            return this;
        }

        #endregion
    }
}
